/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.core.paramloader;

import java.util.ArrayList;
import java.util.List;

import org.jdpf.core.kernel.elements.Token;
import org.jdpf.core.kernel.logging.ILogger;
import org.jdpf.core.kernel.logging.LoggerFactory;
import org.jdpf.core.plugins.datatypes.AbstractDataType;
import org.jdpf.core.plugins.parameters.Parameters;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class ParamEventHandler extends DefaultHandler {
	
	private static final ILogger logger=LoggerFactory.getDefault().getLogger();

	private ParamParserListener listener;
	private Token token;
	private List<AbstractDataType> list;
	private Parameters param;
	private String state;
	private String transitionId;
	private boolean flag;
	private int valueCount;
	private StringBuffer sbValue = new StringBuffer();
	private StringBuffer sbName = new StringBuffer();
	private StringBuffer sbTaskName = new StringBuffer();
	private StringBuffer sbTaskLogging = new StringBuffer();

	public ParamEventHandler(ParamParserListener listener){
		this.listener = listener;
		flag=true;
		valueCount=0;
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String s = new String(ch, start, length);
		s = s.trim();
		if(state.equals("File")){
			// DO nothing
		}
		else if(state.equals("TaskName")){
			transitionId = "org.jdpf.core.contextmanager.Task";
			sbTaskName.append(s);
		}
		else if(state.equals("TaskLogging")){
			transitionId = "org.jdpf.core.contextmanager.Task";
			sbTaskLogging.append(s);
		}
		else if(state.equals("Name")){
			sbName.append(s);
		}
		else if(state.equals("Value")){
			sbValue.append(s);
		}
	}

	@Override
	public void endDocument() throws SAXException {
		if(flag){
			list.add(param);
		}
		token.setValueList(list);
		listener.endParsing(token);
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(localName.equals("Parameter")){
			if(valueCount!=0){
				sbValue=new StringBuffer();
				valueCount=0;
			}
			if(sbValue.length()==0)
				logger.warning(this, "org.jdpf.core.paramloader", "none", 
						"Found an empty value element with name "+sbName.toString()
						+" for transition "+transitionId+".");
			param.setParameterValue(transitionId, sbName.toString().trim(), sbValue.toString().trim());
			sbName = new StringBuffer();
			sbValue = new StringBuffer();
		} else if(localName.equals("TaskLogging") ){
			param.setParameterValue(transitionId, "TaskLogging", sbTaskLogging.toString().trim());	
			sbTaskLogging = new StringBuffer();
		} else if(localName.equals("TaskName")){
			param.setParameterValue(transitionId, "TaskName", sbTaskName.toString().trim());	
			sbTaskName = new StringBuffer();
		} else if(localName.equals("Value")){
			valueCount--;
		}
		state="";
	}

	@Override
	public void startDocument() throws SAXException {
		token = new Token();
		list = new ArrayList<AbstractDataType>();
		param = new Parameters();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if(localName.equals("JDPFParameters")){
		}
		else if(localName.equals("JDPFNet")){
		}
		else if(localName.equals("File")){
		}
		else if(localName.equals("Parameters")){
			transitionId = attributes.getValue("transitionId");
			
			if(attributes.getValue("logging")==null)
				param.setParameterValue(transitionId, "Logging", "false");
			else
				param.setParameterValue(transitionId, "Logging", attributes.getValue("logging"));
		}
		else if(localName.equals("Parameter")){
			sbName = new StringBuffer();
			sbValue = new StringBuffer();
		}
		else if(localName.equals("Name")){
		}
		else if(localName.equals("Value")){
			valueCount++;
		}
		state = localName;
	}
	
	
	public void fatalError(SAXParseException e){
		logger.error(this,"Unable to parse net file "+e.getSystemId());
		logger.error(this,"Error found at line "+e.getLineNumber()+" while parsing "+e.getSystemId());
		logger.error(this,"["+e.getLocalizedMessage()+"]\n");
	}
	
	public void error(SAXParseException e){
		if(e.getMessage().startsWith("cvc-elt")){
			logger.error(this, e.getMessage());
			flag=false;
		}
	}
	

}
